<script setup lang="ts">
import {
  findCommentInfoByGoods,
  findGoodsCommentListById
} from '@/service/api/goods'
import {
  goodsCommentsListParams,
  IEvaluate,
  IGoodsDetail
} from '@/service/api/type/goods.type'
import { computed, inject, reactive, ref } from 'vue'
import { IGoodsCommentInfo } from '@/service/api/type/goods.type'
import GoodsCommentList from './goods-comment-list.vue'
import Pagination from '@/components/pagination/pagination.vue'

const goods = inject<IGoodsDetail>('goods')

const commentInfo = reactive({}) as IEvaluate
const getCommentInfo = async () => {
  const { result } = await findCommentInfoByGoods(goods?.id || '')
  Object.assign(commentInfo, result)
}
getCommentInfo()

const tagsList = computed(() => {
  const tagsList = [
    { title: '全部评价', tagCount: 0 },
    { title: '有图', tagCount: 0 }
  ]
  if (commentInfo?.tags?.length) {
    tagsList[0].tagCount = commentInfo.evaluateCount
    tagsList[1].tagCount = commentInfo.hasPictureCount
    tagsList.push(...commentInfo.tags)
  }
  return tagsList
})

const tagIndex = ref(0)
const changeTag = (index: number) => {
  findCommentListParams.page = 1
  tagIndex.value = index
  if (index === 0) {
    delete findCommentListParams.hasPicture
    delete findCommentListParams.tag
  } else if (index === 1) {
    findCommentListParams.hasPicture = true
  } else {
    delete findCommentListParams.hasPicture
    findCommentListParams.tag = tagsList.value[index].title
  }
  getCommentListData()
}

const sortWay = ref('')
const changeSort = (sort: '' | 'praiseCount' | 'createTime') => {
  findCommentListParams.page = 1
  sortWay.value = sort
  findCommentListParams.sortField = sort
  getCommentListData()
}

const findCommentListParams: goodsCommentsListParams = reactive({
  page: 1,
  pageSize: 10
}
)
const commentListInfo = reactive({}) as IGoodsCommentInfo
const getCommentListData = async () => {
  const { result } = await findGoodsCommentListById(
    goods?.id || '',
    findCommentListParams
  )
  Object.assign(commentListInfo, result)
}
getCommentListData()



const currentPage = computed(() => findCommentListParams.page)
const changePage = (page: number) => {
  findCommentListParams.page = page
  getCommentListData()
}

</script>

<template>
  <div class="goods-comment" v-if="commentInfo.tags">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span>
          <span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span>
          <span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:;"
            v-for="(tag, index) in tagsList"
            @click="changeTag(index)"
            :class="{ active: index === tagIndex }"
          >{{ tag.title }}({{ tag.tagCount }})</a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a href="javascript:;" @click="changeSort('')" :class="{ active: sortWay === '' }">默认</a>
      <a
        href="javascript:;"
        @click="changeSort('createTime')"
        :class="{ active: sortWay === 'createTime' }"
      >最新</a>
      <a
        href="javascript:;"
        @click="changeSort('praiseCount')"
        :class="{ active: sortWay === 'praiseCount' }"
      >最热</a>
    </div>
    <GoodsCommentList v-for="item in commentListInfo.items" :key="item.id" :list="item"></GoodsCommentList>
    <Pagination
      :total="commentListInfo.counts"
      @current-change="changePage"
      :current-page="currentPage"
    ></Pagination>
  </div>
</template>

<style lang="less" scoped>
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @themeColor;
            background: lighten(@themeColor, 50%);
            color: @themeColor;
          }
          &.active {
            border-color: @themeColor;
            background: @themeColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @themeColor;
      }
    }
  }
}
</style>
